<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button @click="batchHandleNext" color="success"> 一键通过</a-button>
        <a-button @click="oneClickRejection" color="error"> 一键驳回</a-button>
      </template>
      <!-- 测试列数据 -->
      <template #userInfo="{ record }">
        <div class="disC">
          <Image :src="record.avatar" :width="60" />

          <div style="margin-left: 10px">
            <div>{{ record.nickName }}</div>
            <div>{{ record.memberId }}</div>
          </div>
        </div>
      </template>
      <template #bindAndCard="{ record }">
        <div>卡种：{{ record.cardTypeName }}</div>
        <div>品牌：{{ record.brandName }}</div>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction
          :actions="getTableAction(record)"
          style="{ width: 400px;
          display: flex;
          flex-direction: column;}"
        />
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }"> </template>
    </BasicTable>
    <!-- 表单区域 -->
    <AssessmentModal @register="registerModal" @success="handleSuccess"></AssessmentModal>
  </div>
</template>

<script lang="ts" name="card-assessment" setup>
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { Image } from 'ant-design-vue';
  import { useListPage } from '/@/hooks/system/useListPage';
  import AssessmentModal from './components/AssessmentModal.vue';
  import { columns, searchFormSchema, superQuerySchema } from './Assessment.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, batchNext, examineOne, rejectionNext } from './Assessment.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '卡券',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '卡券',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */

  function handleViewDetail(record: Recordable) {
    console.log('record', record);
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  /**
   * 驳回和通过
   */
  async function handleChange(record, state) {
    await examineOne({ id: record.id, state }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  // 批量通过
  async function batchHandleNext() {
    await batchNext({ ids: selectedRowKeys.value }, handleSuccess);
  }

  // 批量通过
  async function oneClickRejection() {
    await rejectionNext({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '查看详情',
        onClick: handleViewDetail.bind(null, record, 1),
      },
      {
        label: '通过',
        onClick: handleChange.bind(null, record, 1),
      },
      {
        label: '驳回',
        onClick: handleChange.bind(null, record, 4),
      },
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      // {
      //   label: '详情',
      //   onClick: handleDetail.bind(null, record),
      // },
      {
        label: '通过',
        onClick: handleChange.bind(null, record, 1),
      },
      {
        label: '驳回',
        onClick: handleChange.bind(null, record, 4),
      },
    ];
  }
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
  .treeBox {
    margin: 10px;
    background: #fff;
    padding: 20px;
    box-sizing: border-box;
    .btn {
    }
  }
  .disC {
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
